/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package infoplaces;

//import auxiliares.EquirectangularToCubic;
//import auxiliares.Server;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 *
 * @author Evangelista
 */
public class Panorama {

    private String pano_id = "";
    private double lat = 0;
    private double lng = 0;
    private ArrayList<String> lst_vizinhos = new ArrayList<String>();
    private ArrayList<BufferedImage> lst_images = new ArrayList<BufferedImage>();
    private BufferedImage final_image;
    private String myXML = "";
    private Histograma histograma;
    private double norte;
    
    public BufferedImage get_imagem() {
        return final_image;
    }

    public String get_pano_id() {
        return pano_id;
    }

    public Panorama(String pano_id) {
        this.myXML = Server.getXMLOfURL( pano_id );
        this.pano_id = pano_id;
        this.norte = XML.getNorte(myXML);
        this.lst_vizinhos = XML.getVizinhos(myXML);
        this.lat = XML.getLatitude(myXML);
        this.lng = XML.getLongitude(myXML);
        
        System.out.println(pano_id);
        
//        iniciarVariaveis();
    }
    
    public void iniciarVariaveis(){
        try {
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 2; j++) {
                    lst_images.add(Server.getImageOfURL("http://cbk0.google.com/cbk?output=tile&panoid=" + this.pano_id + "&zoom=2&x=" + i + "&y=" + j));
                }
            }

            
//            for(int j = 0; j < lst_images.size(); j++){
//                salvarPano(lst_images.get(j), "testeDoJonas_" + j + ".png");
//            }
//            
//            salvarPano(juntarImagensArrayList(), "juntarImagem.png");
            
            lst_images = EquirectangularToCubic.processImageFile(juntarImagensArrayList());

            final_image = criarFinalImage();

            salvarPano(final_image, "Panorama_" + this.get_pano_id() +".png");

//            for (int i = 0; i < lst_images.size(); i++) {
//                salvarPano(lst_images.get(i), "imagem_" + i + ".png");
//            }

        } catch (IOException ex) {
            Logger.getLogger(Panorama.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private BufferedImage criarFinalImage() {
        BufferedImage retorno = null;

        try {

            //Largura e Altura de uma imagem
            int width = lst_images.get(0).getWidth();
            int height = lst_images.get(0).getWidth();

            retorno = new BufferedImage(width * 4, height, BufferedImage.TYPE_INT_ARGB);

            Graphics2D g2d = retorno.createGraphics();

            //Desenhamos a primeira linha de imagens
            g2d.drawImage(lst_images.get(0), 0, 0, null);
            g2d.drawImage(lst_images.get(1), width, 0, null);
            g2d.drawImage(lst_images.get(2), width * 2, 0, null);
            g2d.drawImage(lst_images.get(3), width * 3, 0, null);

            g2d.dispose();


        } catch (Exception e) {
            e.printStackTrace();
        }


        return retorno;
    }

    // 
    public void add_image(BufferedImage image) {
        lst_images.add(image);
    }

    public BufferedImage juntarImagensArrayList() {

        BufferedImage retorno = null;

        try {

            //Largura e Altura de uma imagem
            int width = lst_images.get(0).getWidth();
            int height = lst_images.get(0).getWidth();

            BufferedImage dest = new BufferedImage(width * 4, height * 2, BufferedImage.TYPE_INT_ARGB);

            //Obtemos o contexto gráfico da imagem final.  
            Graphics2D g2d = dest.createGraphics();

            //Desenhamos a primeira linha de imagens
            g2d.drawImage(lst_images.get(0), 0, 0, null);
            g2d.drawImage(lst_images.get(2), width, 0, null);
            g2d.drawImage(lst_images.get(4), width * 2, 0, null);
            g2d.drawImage(lst_images.get(6), width * 3, 0, null);

            //Desenhamos a segunda linha de imagens
            g2d.drawImage(lst_images.get(1), 0, height, null);
            g2d.drawImage(lst_images.get(3), width, height, null);
            g2d.drawImage(lst_images.get(5), width * 2, height, null);
            g2d.drawImage(lst_images.get(7), width * 3, height, null);

            //Descartamos o contexto gráfico  
            g2d.dispose();

            //Salvamos a imagem final no disco.  
            //ImageIO.write(dest, "png", new File("panorama_com_repeticao.png"));

            retorno = cortarRepeticao(dest);

            //savarPano(retorno);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return retorno;
    }

    public double criaHistogramaPanorama(){
        histograma = new Histograma(pano_id, final_image.getWidth(), this.norte);
        return histograma.criarHistograma();
    }
    public static void salvarPano(BufferedImage panorama, String path) {

        try {
            ImageIO.write(panorama, "png", new File(path));
            // System.out.println("Panorama salvo com sucesso!");
        } catch (IOException ex) {
            Logger.getLogger(Panorama.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public BufferedImage cortarRepeticao(BufferedImage panorama) {

        int width = 1665;
        int height = 832;

        return panorama.getSubimage(0, 0, width, height);
    }
    
        public ArrayList<String> getLst_vizinhos() {
        return lst_vizinhos;
    }

    public void setLst_vizinhos(ArrayList<String> lst_vizinhos) {
        this.lst_vizinhos = lst_vizinhos;
    }

    public ArrayList<BufferedImage> getLst_images() {
        return lst_images;
    }

    public void setLst_images(ArrayList<BufferedImage> lst_images) {
        this.lst_images = lst_images;
    }

    public BufferedImage getFinal_image() {
        return final_image;
    }

    public void setFinal_image(BufferedImage final_image) {
        this.final_image = final_image;
    }

    public Histograma getHistograma() {
        return histograma;
    }

    public void setHistograma(Histograma histograma) {
        this.histograma = histograma;
    }

    public double getNorte() {
        return norte;
    }

    public void setNorte(double norte) {
        this.norte = norte;
    }

    public double getLat() {
        return lat;
    }

    public void setLat(double lat) {
        this.lat = lat;
    }

    public double getLng() {
        return lng;
    }

    public void setLng(double lng) {
        this.lng = lng;
    }
    
        public int lerNumeroMatches() {
        File arq = new File("temp/" + pano_id + "_matches.txt");

        FileReader file;
        try {
            file = new FileReader(arq);
            BufferedReader buf = new BufferedReader(file);
            return Integer.parseInt(buf.readLine());
        } catch (FileNotFoundException ex) {
            System.out.println("ARQUIVO DE MATCHES NAO ENCONTRADO !");
        } catch (IOException io) {
            System.out.println("ERRO NO CARREGAMENTO DO ARQUIVO DE MATCHES !");
        }
        return -1;
    }
}
